const app = getApp()
var starsCount = 0;
// pages/order/order.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currtab: 0,
    swipertab: [{
      name: '待接单', orderStatus: 0}, { name: '已接单', orderStatus: 1}, { name: '已完成', orderStatus: 2 }, { name: '已取消', orderStatus: 3 }],
    orderList0:"",
    orderList1: "",
    orderList2: "",
    orderList3: "",

    /////弹框
    modalHidden: true,
    modalHidden0: true,
    modalHidden1: true,
    ///////评分
    stars: [0, 1, 2, 3, 4],
    normalSrc: '../../../images/star-no.png',
    selectedSrc: '../../../images/star-full.png',
    halfSrc: '../../../images/star-half.png',
    key: 0,//评分
    status: '', 
    pingFenOrderId:"",
    id:""
    
  },

  //////////////////////////评分的方法
  selectLeft: function (e) {
    var key = e.currentTarget.dataset.key
    if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
      //只有一颗星的时候,再次点击,变为0颗
      key = 0;
    }
    starsCount = key
    this.setData({
      key: key
    })

  },  /**
   * 点击右边,整颗星   */
  selectRight: function (e) {
    var key = e.currentTarget.dataset.key
    starsCount = key
    this.setData({
      key: key
    })
  },
  // 确定按钮
  startRating: function (e) {
    var that = this;
    wx.showModal({
      title: '分数',
      content: "" + starsCount,
      success: function (res) {
        if (res.confirm) {          
          console.log('用户点击确定')
          var orderId = that.data.pingFenOrderId
          var id = that.data.id
          console.log('订单编号：', orderId)
          console.log('评分：', starsCount)
          console.log('id：', id)
          wx.request({
            url: app.globalData.baseUrl + '/order/setpingfen',
            method: 'POST',
            data: {
               "orderId": orderId,
               "pingFen": starsCount,
               "id":id
             },//传入的参数
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
              var result = res.data.success;

              console.log("result:", result)
              if (result == false) {
                var toastText = '无数据';
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
              } else {
                console.log('评分成功！')
                var toastText = '评分成功！';
                wx.showToast({
                  title: toastText,
                  icon: '',
                  duration: 2000
                });
                
                /*that.setData({
                  worker: worker,
                  url: app.globalData.baseUrl + worker.picture,
                  modalHidden: false
                });*/
              }
            }
          })
        }
        
      }
    })
  },


  ////////////////////////

  ///////////////////评分信息弹框
  modalTap1: function (e) {
    var that = this
    var id = e.currentTarget.dataset.wid
    var name = e.currentTarget.dataset.wname
    var phone = e.currentTarget.dataset.wphone
    var orderId = e.currentTarget.dataset.orderid0
    that.setData({
      id:id,
      name:name,
      phone:phone,
      pingFenOrderId: orderId,
      url3: "../../../images/happy1.png",
      url4: "../../../images/s.png",
      modalHidden1: false
    });
    /////////////////////////如果已评分则展示评分结果
    wx.request({
      url: app.globalData.baseUrl + '/order/getorderpingfen',
      method: 'GET',
      data: { "orderId": orderId },//传入的参数
      success: function (res) {
        var orderPingFen = res.data.orderPingFen;

        console.log("orderPingFen", orderPingFen)
        if (orderPingFen == null) {
          var toastText = '未评分';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            key: orderPingFen
            
          });
          starsCount= orderPingFen;
        }
      }
    })


    ///////////////////////////////

  },
  modalHide1: function (e) {
   
    this.setData({
      modalHidden1: true,
      ///////评分
      key:0,
      starsCount:0
      //////
    })
  },
  /*cancelscore: function (e) {
    this.setData({
      modalHidden1: true
    })
  },*/
  
  ////////////

  ///////////////////理由信息弹框
  modalTap0: function (e) {
    var that = this
    var remark = e.currentTarget.dataset.remark
    var date = e.currentTarget.dataset.date
    console.log("remark:", remark)  
    if (remark==null){
      remark:"用户未说明理由"
    }
          that.setData({
            remark: remark,
            date:date,
            purl1: "../../../images/why.png",
            purl2: "../../../images/sad.png",
            modalHidden0: false
          });   
  },
  modalHide0: function (e) {
    this.setData({
      modalHidden0: true
    })
  },
  ////////////

///////////////////师傅信息弹框
  modalTap: function (e) {
    var that = this
    var id = e.currentTarget.dataset.id
    console.log("id:",id)
    wx.request({
      url: app.globalData.baseUrl + '/worker/getworkerbyid',
      method: 'GET',
      data: { "id": id },//传入的参数
      success: function (res) {
        var worker = res.data.worker;

        console.log("worker", worker)
        if (worker == null) {
          var toastText = '无数据';
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            worker: worker,
            url: app.globalData.baseUrl+worker.picture,
            modalHidden: false
          });
        }
      }
    })
  },
  modalHide: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  ////////////

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo()
    this.orderShow()
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    this.setData({ currtab: e.detail.current })
    this.orderShow()
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        that.daijiedanShow()
        break
      case 1:
        that.yijiedanShow()
        break
      case 2:
        that.alreadyShow()
        break
      case 3:
        that.lostShow()
        break
    }
  },

  daijiedanShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid ,orderStatus:0},//传入的参数
      success: function (res) {
        var list = res.data.orderList;
        
        console.log("list", list)
        if (list == null) {
          var toastText = '无数据' ;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList0: list
          });
        }
      }
    })
    
  },
  yijiedanShow: function (){
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid, orderStatus: 1 },//传入的参数
      success: function (res) {
        var list = res.data.orderList;

        console.log("list", list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList1: list
          });
        }
      }
    })
  },
  alreadyShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid, orderStatus: 2 },//传入的参数
      success: function (res) {
        var list = res.data.orderList;

        console.log("list", list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList2: list
          });
        }
      }
    })
  },

  lostShow: function () {
    var that = this
    //页面初始化，option为页面跳转所带来的参数
    let openid = wx.getStorageSync("openid");
    //var openid = options.openid
    console.log("openid", openid)
    wx.request({
      url: app.globalData.baseUrl +'/order/findorderbyopenid',
      method: 'GET',
      data: { "buyerOpenid": openid, orderStatus: 3 },//传入的参数
      success: function (res) {
        var list = res.data.orderList;

        console.log("list", list)
        if (list == null) {
          var toastText = '获取数据失败' + res.data.errMsg;
          wx.showToast({
            title: toastText,
            icon: '',
            duration: 2000
          });
        } else {
          that.setData({
            orderList3: list
          });
        }
      }
    })
  },
  cancel:function(e){
    var that = this;
    console.log("orderId:" + e.currentTarget.dataset.orderid)
    wx.showModal({
      title: '提示',
      content: '确定要取消该订单吗？',

      success: function (sm) {
        if (sm.confirm) {
          wx.request({
            url: app.globalData.baseUrl +'/order/cancel',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: { "orderId": e.currentTarget.dataset.orderid },

            
            
            success: function (res) {
              var result = res.data.success;
              var toastText = "取消成功！";
              if (result != true) {
                toastText = "取消失败";
              } else {
                
                that.daijiedanShow();
              }
              wx.showToast({
                title: toastText,
                icon: '',
                duration: 2000
              });
            }
          })
        }
      }
    })
  },
  getDetail:function(e){
    var url1 = '../../order/orderdetail/orderdetail?orderId=' + e.currentTarget.dataset.orderid
    console.log("url1:" + url1)
    wx.navigateTo({
      url: url1
    })

  },

  // 下拉刷新
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    var that = this;
    that.setData({
      
      orderList0: null,
      orderList1: null,
      orderList2: null,
      orderList3: null
    });
    setTimeout(function () {
      that.orderShow()
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    }, 1500);

  },

  ////////////////////拨号
  Tel: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel, //这个是我的手机号，模拟测试
      success: function () {
        console.log("拨打电话成功！")
      },
      fail: function () {
        console.log("拨打电话失败！")
      }
    })
  }
  


})
